require "common/Define"
require "framework/BaseController"
require "FairyGUI"

GestureMain = BaseController:New({mControllerName = "GestureMain"});

function GestureMain.Main()
	-- body
	GestureMain:Awake();
end

function GestureMain:Awake()
	Application.targetFrameRate = 60;
	Stage.inst.onKeyDown:Add(self.OnKeyDown, self);

	-- attack pacakge, package in resource or assetbundle
	local packageName = "UI/Gesture";
	UIPackage.AddPackage(packageName);

	local componentName = "Main";
	self.mainComponent = UIPackage.CreateObject("Gesture", componentName).asCom;
	GRoot.inst:AddChild( self.mainComponent );

	--local go = GameObject();
	--local panel = go:AddComponent(typeof(UIPanel));
	--panel.container:AddChild(self.mainComponent);
	--GRoot.inst:AddChild( panel );

	-- self.mainComponent:SetSize(GRoot.inst.width, GRoot.inst.height);
	-- self.mainComponent:AddRelation(GRoot.inst, RelationType.Size);
	GRoot.inst:SetContentScaleFactor(1134, 640, FairyGUI.UIContentScaler.ScreenMatchMode.MatchWidthOrHeight);

	self:OnViewCreated(self.mainComponent);
end

function GestureMain:InitView()
	-- show earth
	local earth = GameObject.Find("Globe");
	earth:GetComponent(typeof(UnityEngine.MeshRenderer)).enabled = true;

	local holder = self.mainComponent:GetChild("holder");

	self._ball = GameObject.Find("Globe").transform;

	local gesture1 = FairyGUI.SwipeGesture(holder);
	gesture1.onMove:Add(self.OnSwipeMove, self);
	gesture1.onEnd:Add(self.OnSwipeEnd, self);

	local gesture2 = FairyGUI.LongPressGesture(holder);
	gesture2.once = false;
	gesture2.onAction:Add(self.OnHold, self);

	local gesture3 = FairyGUI.PinchGesture(holder);
	gesture3.onAction:Add(self.OnPinch, self);

	local gesture4 = FairyGUI.RotationGesture(holder);
	gesture4.onAction:Add(self.OnRotate, self);
end

function GestureMain:OnSwipeMove(context)
	-- body
	local gesture = context.sender;
	local v = Vector3();
	if (Mathf.Abs(gesture.delta.x) > Mathf.Abs(gesture.delta.y)) then
		v.y = -Mathf.Round(gesture.delta.x);
		if (Mathf.Abs(v.y) < 2) then-- 消除手抖的影响
			return;
		end
	else
		v.x = -Mathf.Round(gesture.delta.y);
		if (Mathf.Abs(v.x) < 2) then
			return;
		end
	end
	self._ball:Rotate(v, UnityEngine.Space.World);
end

function GestureMain:OnSwipeEnd(context)
	-- body
	local gesture = context.sender;
	local v = Vector3();
	if (Mathf.Abs(gesture.velocity.x) > Mathf.Abs(gesture.velocity.y)) then
		v.y = -Mathf.Round(Mathf.Sign(gesture.velocity.x) * Mathf.Sqrt(Mathf.Abs(gesture.velocity.x)));
		if (Mathf.Abs(v.y) < 2) then
			return;
		end
	else
		v.x = -Mathf.Round(Mathf.Sign(gesture.velocity.y) * Mathf.Sqrt(Mathf.Abs(gesture.velocity.y)));
		if (Mathf.Abs(v.x) < 2) then
			return;
		end
	end
	self._ball:DORotate(v, 0.3, DG.Tweening.RotateMode.WorldAxisAdd);
end

function GestureMain:OnHold(context)
	-- body
	self._ball:DOShakePosition(0.3, Vector3(0.1, 0.1, 0));
end

function GestureMain:OnPinch(context)
	-- body
	DOTween.Kill(_ball);

	local gesture = context.sender;
	local newValue = Mathf.Clamp(_ball.localScale.x + gesture.delta, 0.3, 2);
	self._ball.localScale = Vector3(newValue, newValue, newValue);
end

function GestureMain:OnRotate(context)
	-- body
	DOTween.Kill(_ball);

	local gesture = context.sender;
	self._ball:Rotate(Vector3.forward, -gesture.delta, UnityEngine.Space.World);
end

function GestureMain:OnKeyDown(context)
 	-- body
 	if (context.inputEvent.keyCode == KeyCode.Escape) then
		print "Exit Game!"
		Application.Quit();
	end
end

